package com.lft.examsys.controller;

import com.lft.examsys.annotation.AuthorizeName;
import com.lft.examsys.entity.Item;
import com.lft.examsys.entity.Type;
import com.lft.examsys.model.IModel;
import com.lft.examsys.service.ItemService;
import com.lft.examsys.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Autowired
    private TypeService typeService;


    private static String elName = "itemes";


    @GetMapping("/manage")
    @PreAuthorize("hasAnyAuthority('item.manage')")
    @AuthorizeName("题目管理")
    public ModelAndView manage(IModel model, Integer page, Integer size) {
        return model.validation("admin/ItemManage", () -> {
            //TODO: 题目管理 -DONE
            itemService.getAllByPage(model,page, size, elName);
        }, model::err);
    }


    @GetMapping("/edit/{id}")
    @PreAuthorize("hasAnyAuthority('item.edit')")
    public ModelAndView editItem(IModel model, @PathVariable Integer id, Integer page, Integer size) {
            return model.validation("admin/ItemEdit", () -> {
                Item item = itemService.getItem(id);
                List<Type> types = typeService.getTypeNames();
                model.add("item",item);
                model.add("types", types);
                model.add("page", page);
                model.add("size", size);
            }, model::err);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('item.update')")
    public ModelAndView updateItem(IModel model, Item item, Integer page, Integer size) {
        return model.validation("redirect:/item/manage?page=" + page +"&size=" + size, () -> {
            itemService.saveItem(item);
        }, model::err);
    }

    @PostMapping("/delete")
    @PreAuthorize("hasAnyAuthority('item.delete')")
    public ModelAndView deleteItem(IModel model, Integer id, Integer page, Integer size) {
        return model.validation("redirect:/item/manage?page=" + page +"&size=" + size, () -> {
            if (itemService.deleteItem(id) == 1) {

            } else {
                model.err("删除题目失败");
            }
        }, model::err);
    }

    @GetMapping("/add")
    @PreAuthorize("hasAnyAuthority('item.toadd')")
    public ModelAndView addIndex(IModel model) {
        return model.validation("admin/ItemAdd", () -> {
            List<Type> types = typeService.getTypeNames();
            model.add("types", types);
        }, model::err);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('item.add')")
    public ModelAndView addItem(IModel model,  Integer page, Integer size, Item item) {
        return model.validation("admin/ItemManage", () -> {
            if (itemService.addItem(item) == 1) {
                itemService.getAllByPage(model,page, size, elName);
            } else {
                model.err("添加失败！");
            }
        }, model::err);
    }

    // 自动添加题目
    @GetMapping("/test")
    public ModelAndView addValues(IModel model) {
        return model.validation("/index", () -> {
            String[] answers = {"A", "B", "C", "D"};
            for (int i = 1; i <= 10; i++) {
                String title = "这是第" + i + "题";
                String content = "A.1 B.2 C.3 D.4";
                Integer random = (int) (Math.random() * 4);
                String answer = answers[random];
                String type = "single";
                itemService.test(title,content,answer,type);
            }
        }, model::err);
    }
}
